import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
public class MysqlImageSave {
	public static Connection sqlCon = null;
	 String url = "jdbc:mysql://localhost:3306/";
	 String dbName = "pichubdb";
	  String driver = "com.mysql.jdbc.Driver";
	  String userName = "pichub"; 
	  String password = "Mark1990";
	
	
	 MysqlImageSave(){
		 connectToSQL();
		 //saveImage("C:\\Users\\Administrator\\Pictures\\bus_ad.jpg");
		 updateCity("West Lafayette, IN",1);
	 }
	 
	 private void connectToSQL(){
		 try {
		  Class.forName(driver).newInstance();
		  sqlCon = DriverManager.getConnection(url+dbName,userName,password);
		  System.out.println("CONNECTED TO DATABASE pichubdb");
		  //sqlCon.close();
		  //System.out.println("DISCONNECTED FROM DATABASE pichubdb");
		 }catch(Exception e){			 
		 }
		 
	}
	 
	  private void saveImage(String imageName){
	    	try{
	    		File myImage = new File(imageName);
	    		FileInputStream myImageStream = new FileInputStream( myImage );
	    		PreparedStatement myPrepareStatement = sqlCon.prepareStatement("INSERT INTO image(imageData) VALUES (?)");
	    		myPrepareStatement.setBinaryStream(1, (InputStream)myImageStream, (int)(myImage.length()));
	    		myPrepareStatement.executeUpdate();
	    	}catch(Exception e){
	    		System.err.println(e);
	    		System.err.println("Image Error");
	    	}
	    }
	  
	  private void updateCity(String cityName,int id){
	    	try{
	    		Statement st = sqlCon.createStatement();
	    		st.execute("UPDATE image id,city VALUES('"+id+"','" + cityName + "');");
	    	}catch(Exception e){
	    		System.err.println(e);
	    		System.err.println("Image Error");
	    	}
	    }
	  public static void main(String[] args){
		  new MysqlImageSave();
		 
	  }
}
